# coding=gbk
"""
Created on 2016/6/15 0015

@author: Blunce

@about:

"""
from itertools import count, islice
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
import random

PATH_FILES = os.getcwd()[:-5] + "data" + os.sep + "preprocess_data" + os.sep
PATH_TMP = os.getcwd()[:-5] + "data" + os.sep + "ex06" + os.sep


class Ex06:
    def __init__(self):
        pass

    def show_9_ECG(self):
        all_files = os.listdir(PATH_FILES)
        dic = {}
        for one in all_files:
            if one != "count_all_abnormal.csv":
                dic[one] = self.__read_one_line(PATH_FILES + one)

        fig = plt.figure()
        ax = fig.add_subplot(111)
        for key in dic.keys():
            ax.plot(dic[key])
        fig.savefig(PATH_TMP + "9_ecg_show.png")

    def show_each_ecg(self):
        all_files = os.listdir(PATH_FILES)
        dic = {}
        for one in all_files:
            if one != "count_all_abnormal.csv":
                dic[one] = self.__read_one_line(PATH_FILES + one)

        for key in dic.keys():
            fig = plt.figure()
            ax = fig.add_subplot(111)
            ax.plot(dic[key])
            fig.savefig(PATH_TMP + key[:-3] + "png")

    def show_each_random_ecg(self):
        all_files = os.listdir(PATH_FILES)
        dic = {}
        for one in all_files:
            if one != "count_all_abnormal.csv":
                dic[one] = self.__read_random_line(PATH_FILES + one)

        for key in dic.keys():
            fig = plt.figure()
            ax = fig.add_subplot(111)
            ax.plot(dic[key])
            fig.savefig(PATH_TMP + key[:-3] + "png")

    def __read_one_line(self, filename):
        with open(filename) as fr:
            line = fr.readline()
            x = line.split(',')
            return np.array(x)

    def __read_random_line(self, filename):
        with open(filename) as fr:
            lines = fr.readlines()
            random.shuffle(lines)
            for line in lines:
                if len(line) > 2:
                    x = line.split(',')
                    return np.array(x)

        return None


if __name__ == '__main__':
    print "**************start**************\n\n"

    # Ex06().show_9_ECG()
    # Ex06().show_each_ecg()
    Ex06().show_each_random_ecg()

    print "\n\n**************end**************"
